"""
table.py --                                                                                            
                                                                                                                                                                                                        
It sets a table's geometric properties.                                              
                                                                                                                                                                                                            
Date of creation: 2007-03-17                                                                        
                                                                                                                                                                                                
Copyright  Robotics and Automation Group, Pontificia Universidad Javeriana - Cali.                  
    Freddy Naranjo Perez, fnaranjo@puj.edu.co                                                                   
    Antonio Alejandro Matta Gomez amatta@puj.edu.co                                      
    Julian David Colorado, jdcolorado@puj.edu.co                           
    Juan Camilo Acosta Mejia, jcacosta@puj.edu.co                                    
                                                                                                                                                                                                        
See the file "license.terms" for information on usage and redistribution of this file, and for a    
DISCLAIMER OF ALL WARRANTIES.
"""

import vtk
from vtk.util.colors import *

class Table:
    def __init__( self, ren, geomdim, posorient, lighting, color ):
        """ Inits a table's figure and its associated texture.
            params: length, height, width, xpos, ypos, zpos   
       """
        #offsets
        xoffset = geomdim[0]/20
        yoffset = geomdim[1]-geomdim[1]/10
        zoffset = geomdim[2]/20

        # Create table geometry
        #Base Mesh
        #base points.
        basepoints = vtk.vtkPoints()
        basepoints.InsertPoint(0, 0.0, 0.0, 0.0)
        basepoints.InsertPoint(1, 0.0, geomdim[1], 0.0)
        basepoints.InsertPoint(2, geomdim[0], geomdim[1], 0.0)
        basepoints.InsertPoint(3, geomdim[0], 0.0, 0.0)
        basepoints.InsertPoint(4, 0.0, 0.0, geomdim[2])
        basepoints.InsertPoint(5, 0.0, geomdim[1], geomdim[2])
        basepoints.InsertPoint(6, geomdim[0], geomdim[1], geomdim[2])
        basepoints.InsertPoint(7, geomdim[0], 0.0, geomdim[2])
        basepoints.InsertPoint(8, 0.0, geomdim[1]/2, 0.0)
        basepoints.InsertPoint(9, geomdim[0]/3, geomdim[1], 0.0)
        basepoints.InsertPoint(10, geomdim[0], geomdim[1]/2, 0.0)
        basepoints.InsertPoint(11, geomdim[0]-geomdim[0]/3, geomdim[1], 0.0)
        basepoints.InsertPoint(12, 0.0, geomdim[1]/2, geomdim[2])
        basepoints.InsertPoint(13, geomdim[0]/3, geomdim[1], geomdim[2])
        basepoints.InsertPoint(14, geomdim[0], geomdim[1]/2, geomdim[2])
        basepoints.InsertPoint(15, geomdim[0]-geomdim[0]/3, geomdim[1], geomdim[2])

        #base's cell array
        #The cell array is a connectivity list. The number of points are 
        #specified followed by that number of point
        #ids. This can be repeated as many times as there are primitives in
        #the list.
        basecell = vtk.vtkCellArray()
        basecell.InsertNextCell(4) # cells' number of points
        basecell.InsertCellPoint(0)
        basecell.InsertCellPoint(1)
        basecell.InsertCellPoint(2)
        basecell.InsertCellPoint(3)
        basecell.InsertNextCell(4)
        basecell.InsertCellPoint(4)
        basecell.InsertCellPoint(5)
        basecell.InsertCellPoint(6)
        basecell.InsertCellPoint(7)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(1)
        basecell.InsertCellPoint(5)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(2)
        basecell.InsertCellPoint(6)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(8)
        basecell.InsertCellPoint(9)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(10)
        basecell.InsertCellPoint(11)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(12)
        basecell.InsertCellPoint(13)
        basecell.InsertNextCell(2)
        basecell.InsertCellPoint(14)
        basecell.InsertCellPoint(15)
        #both points and cells are associated in a PolyData set.
        profile = vtk.vtkPolyData()
        profile.SetPoints(basepoints)
        profile.SetLines(basecell)

        # Give shape to the resulting line via the Tube Filter.
        profileTubes = vtk.vtkTubeFilter()
        profileTubes.SetNumberOfSides(20)
        profileTubes.SetInput(profile)
        profileTubes.SetRadius(geomdim[3])
        profileTubes.SetVaryRadiusToVaryRadiusByVector ()
        
        #Create a map of Polygonal type and associate it to
        # the profileTubes.
        map = vtk.vtkPolyDataMapper()
        map.SetInputConnection(profileTubes.GetOutputPort())
        map.ScalarVisibilityOff()

        self.baseActor = vtk.vtkActor()
        self.baseActor.SetMapper(map)
        self.baseActor.GetProperty().SetDiffuseColor(slate_grey)
        self.baseActor.GetProperty().SetSpecular(.3)
        self.baseActor.GetProperty().SetSpecularPower(30)
        
        #Table Top
        #top points
        toppoints = vtk.vtkPoints()
        toppoints.InsertPoint(0, 0.0, 0.0, 0.0)
        toppoints.InsertPoint(1, 0.0, 0.0, geomdim[2])
        toppoints.InsertPoint(2, geomdim[0]-.5, 0.0, 0.0)
        toppoints.InsertPoint(3, geomdim[0]-.5, 0.0, geomdim[2])
        toppoints.InsertPoint(4, geomdim[0], 0.0, 0.0)
        toppoints.InsertPoint(5, geomdim[0], 0.0, geomdim[2])

        # The cell array is a connectivity list. For each cell, the number of points is defined 
        # followed by the point ids. This is repeated as many times as there are primitives in
        # the list.
        topstrip = vtk.vtkCellArray()
        topstrip.InsertNextCell(6) # number of points
        topstrip.InsertCellPoint(0)
        topstrip.InsertCellPoint(1)
        topstrip.InsertCellPoint(2)
        topstrip.InsertCellPoint(3)
        topstrip.InsertCellPoint(4)
        topstrip.InsertCellPoint(5)
        profile = vtk.vtkPolyData()
        profile.SetPoints(toppoints)
        profile.SetStrips(topstrip)

        #Now the Implicit Modeller and the Contour Filter are used in order
        #to to model the top Strip Cell Array into the figure that we need.
        topimp = vtk.vtkImplicitModeller()
        topimp.SetInput(profile)
        topimp.SetSampleDimensions(40, 10, 40)
        topimp.SetMaximumDistance(0.25)
        topimp.SetModelBounds(-0.7, geomdim[0]+0.1, 0.0, 0.001, 0.0, geomdim[2] )

        contour = vtk.vtkContourFilter()
        contour.SetInputConnection(topimp.GetOutputPort())
        contour.SetValue(0, 0.06)
        
        #Smoothing applied in order to get a better surface.
        smooth= vtk.vtkSmoothPolyDataFilter()
        smooth.SetInputConnection(contour.GetOutputPort())
        smooth.SetNumberOfIterations(20)
        
        #Normal's computation.
        normals = vtk.vtkPolyDataNormals()
        normals.SetInputConnection(smooth.GetOutputPort())
        normals.FlipNormalsOn()
        
        #Normal's passed to the poly data mapper.
        topMapper = vtk.vtkPolyDataMapper()
        topMapper.SetInputConnection(smooth.GetOutputPort())
        topMapper.ScalarVisibilityOff()
        
         # Load in the texture map. A texture is any unsigned char image. If it
        # is not of this type, you will have to map it through a lookup table
        # or by using vtkImageShiftScale.
        self.topActor = vtk.vtkActor()
        self.topActor.SetMapper(topMapper)
        
        
        self.topActor.SetPosition( 0.0, geomdim[1]+0.02, 0.0 )
        self.topActor.GetProperty().SetDiffuseColor(banana)
        self.topActor.GetProperty().SetSpecular(.3)
        self.topActor.GetProperty().SetSpecularPower(30)
        #Type of representation.
        #self.topActor.GetProperty().SetRepresentationToSurface ()
        #self.topActor.GetProperty().SetInterpolationToFlat( )
        """ 
        Lighting Properties.
        self.topActor.GetProperty().SetOpacity( lighting[0] )
        self.topActor.GetProperty().SetAmbient( lighting[1] )
        self.topActor.GetProperty().SetDiffuse(  lighting[2] )
        self.topActor.GetProperty().SetSpecular(  lighting[3] )
        self.topActor.GetProperty().SetSpecularPower(  lighting[4] )     
        Color Properties.
        self.topActor.GetProperty().SetAmbientColor( color[0] )
        self.topActor.GetProperty().SetDiffuseColor( color[1] )
        self.topActor.GetProperty().SetSpecularColor( color[2] )
    """
        #encapsulate everything into an assembly
        self.tableassembly = vtk.vtkAssembly()
        self.tableassembly.SetPosition( posorient[0], posorient[1], posorient[2] )
        self.tableassembly.SetOrientation( posorient[3], posorient[4], posorient[5] )
        self.tableassembly.AddPart(self.topActor)
        self.tableassembly.AddPart(self.baseActor)
        
    
        
    
    

        
        
            
        
    

    
  
  